		<section>
			<ul class="breadcrumb">
			  <li><a href="<?php echo base_url('index.php/mainController/'); ?>">Home</a> <span class="divider">/</span></li>
        <li><a href="<?php echo base_url('index.php/borrowingSystemController/manageFile/'); ?>">จัดการแฟ้มสำนวนคดี</a> <span class="divider">/</span></li>
			  
        <?php  

        if(isset($idItem)){
          echo '<li class="active">แก้ไขแฟ้มสำนวนคดี</li>';
        }else{
          echo '<li class="active">สร้างแฟ้มสำนวนคดี</li>';
        }
        ?>
        
			</ul>

      <div class="page-header">
      	
        <?php  
        if(isset($idItem)){
          echo '<h1>แก้ไขแฟ้มสำนวนคดี</h1>';
        }else{
          echo '<h1>สร้างแฟ้มสำนวนคดี</h1>';
        }
        ?>
     	</div>

      <form class="form-horizontal">

        <?php  
        // $caseid = '';
        // $casename = '';
        // if (isset($rs)) {
        //   foreach ($rs as $key => $value) {
        //     $caseid = $value['file_caseid'];
        //     $casename = $value['file_casename'];
        //   }
        // }
        
        ?>

        <div id="alert" class="alert alert-error hide" >
          <strong>เกิดข้อผิดพลาด!</strong> <span></span>
        </div>

        <div id="alert_success" class="alert alert-success hide" >
          <strong>บันทึกเรียบร้อย!</strong> <span></span>
        </div>


          <div class="control-group ">
            <label class="control-label" for="case_id">รหัสคดี</label>
            <div class="controls form-inline">
              <input type="text" id="case_id" name="case_id" placeholder="รหัสคดี" value=""  >
              <span class="help-inline"></span>
              <button style="<?php if(isset($rs)) {echo 'display:none;';} ?>">ค้นหา</button>
            </div>
          </div>

          <div class="control-group ">
            <label class="control-label" for="case_name">ชื่อคดี</label>
            <div class="controls">
              <input type="text" id="case_name" name="case_name" placeholder="ชื่อคดี" value="" >
            </div>
          </div>


        <div class="control-group" style="<?php if(isset($rs)) {echo 'display:none;';} ?>">
          <div class="controls">
            <button type="button" id="btnClear" class="btn">ยกเลิก</button>
            <button type="button" id="btnSave" class="btn btn-primary">บันทึก</button>
          </div>
        </div>

      </form>

        <div style="overflow:auto;" >
          <table id='tbFileCase' class="table table-striped table-condensed" >
            <button id="addRow" type="button" class="btn btn-primary pull-right"><i class="icon-plus-sign icon-white"></i> เพิ่มแฟ้มใหม่</button>
            <thead>
              <tr>
                <th>รหัสแฟ้ม</th>
                <th>ชื่อแฟ้ม</th>
                <th>กลุ่มแฟ้ม</th>
                <th>รหัสารบัญ</th>
                <th>คำอธิบายแฟ้ม</th>
                <?php if(!isset($rs)) ?><th>ดำเนินการ</th>
              </tr>
            </thead>
            <tbody>

              <?php  
              // if (isset($rs)) {
              //   foreach ($rs as $key => $value) {
              //     echo "<tr>";
              //     echo "<td><input type='text' class='span1' value='".iconv('TIS-620', 'UTF-8', $value['document_id'])."' disabled='disabled'></td>";
              //     echo "<td><input type='text' class='span2' value='".iconv('TIS-620', 'UTF-8', $value['document_name'])."' disabled='disabled'></td>";
              //     echo "<td><input type='text' class='span2' value='".iconv('TIS-620', 'UTF-8', $value['document_group'])."' disabled='disabled'></td>";
              //     echo "<td><input type='text' class='span1' value='".iconv('TIS-620', 'UTF-8', $value['index_code'])."' disabled='disabled'></td>";
              //     echo "<td><input type='text' class='span2' value='".iconv('TIS-620', 'UTF-8', $value['document_detail'])."' disabled='disabled'></td>";
              //     echo "<td class='span1'>".anchor('','ลบ')."</td>";
              //     echo "</tr>";
              //   }
              // }else{
              //   echo '<tr>';
              //   echo '<td><input type="text" class="span1"></td>';
              //   echo '<td><input type="text" class="span2"></td>';
                
              //   echo '<td><select class="span2">';
              //   foreach ($list_groupfile as $key => $value) {
              //     echo '<option value="'.iconv('TIS-620', 'UTF-8', $value['ref_name']).'">'.iconv('TIS-620', 'UTF-8', $value['ref_name']).'</option>';
              //   }
              //   echo '</select></td>';
              //   echo '<td><input type="text" class="span1"></td>';
              //   echo '<td><input type="text" class="span2"></td>';
              //   echo '<td><a>เพิ่ม</a></td>';

              //   echo '</tr>';
              // }
              
              ?>

            </tbody>
          </table>
        </div>


    </section>

    <script type="text/javascript">

    var listCaseFile = <?php if(isset($jsondata)) echo $jsondata; else echo '""'; ?>;
    if (listCaseFile != '') {
      $('#case_id').val(listCaseFile[0].file_caseid);
      $('#case_name').val(listCaseFile[0].file_casename);
      $('#case_id').attr('disabled','disabled');
      $('#case_name').attr('disabled','disabled');
      $('#tbFileCase').show();
    }else{
      $('#tbFileCase').hide();
      $('#addRow').hide();
    }

    setDataToTable(listCaseFile);
    
    function setDataToTable(list){
      var content = '';
      $.each(list ,function(key,value){
        content += "<tr>";
        content += "<td>"+value.document_id+"</td>";
        content += "<td>"+value.document_name+"</td>";
        content += "<td>"+value.document_group+"</td>";
        content += "<td>"+value.index_code+"</td>";
        content += "<td>"+value.document_detail+"</td>";
        content += "<td><a data-identify='"+value.document_id+"' class='del'>ลบ</a></td>";
        content += "</tr>";
      });

      $("#tbFileCase tbody").html(content);
    }

    function getDataList(){
      $.ajax({
        type : 'POST',
        url : '<?php echo base_url(); ?>index.php/borrowingSystemController/getListCase',
        data : { id : <?php if (isset($idItem)) { echo $idItem; } else echo "''";?> },
        dataType : 'json',
        success : function(data){
          //alert(data[0].document_id);
          setDataToTable(data);
        },
        error : function(error){
          alert('error : '+error);
        }
      });
    }


      $(function(){
        
        $(document).on("click", '.sub_add', function(){ 
          var obj = []; 
          $(this).closest('tr').find("input,select").each(function(value) {
              obj.push($(this).val());
          });

          // getDataList();
          
          $.ajax({
            type: "POST",
            url: '<?php echo base_url(); ?>index.php/borrowingSystemController/sub_addFileDoc',
            data: {newfile : obj , fid : <?php if(isset($idItem)) echo $idItem; else echo "''"; ?>},
            success: function(data){
              //alert(data);
              if (data == 'found') {
                $('#alert').removeClass('hide');
                $('#alert_success').addClass('hide');
                $('#alert').find('span').html('มีแฟ้มรายคดีนี้อยู่แล้ว');
              }else{
                getDataList();
                $('#alert_success').removeClass('hide');
                $('#alert').addClass('hide');
              }
            
            }
          });
        });

        var listGroup = <?php if(isset($textListGroupFile)) echo json_encode($textListGroupFile); else echo '""'; ?>;

        $('#addRow').bind('click',function(){
          var newContent = '';
          newContent += "<tr>";
          // newContent += "<td><input name='fileid' type='text' class='span2'></td>";
          newContent += "<td></td>"
          newContent += "<td><input name='filename' type='text' class='span2'></td>";
          newContent += "<td><select class='span2'>";
          $.each(listGroup, function(index, value) {
            newContent += "<option value="+value.ref_name+">"+value.ref_name+"</option>";
          });
          newContent += "</select></td>";
          newContent += "<td><input name='indexCode' type='text' class='span1'></td>";
          newContent += "<td><input name='docDetail' type='text' class='span2'></td>";
          newContent += "<td><a class='sub_add'>เพิ่ม</a></td>";
          
          $('#tbFileCase tbody').append(newContent);
          
        });

        $('#btnSave').bind('click',function(){
          if ( $.trim($('#case_id').val()) != "" && $.trim($('#case_name').val()) ) {
            $('#alert').addClass('hide');
            $.ajax({
              type : 'POST',
              url : '<?php echo base_url(); ?>index.php/borrowingSystemController/addCase',
              data : { case_id : $.trim($('#case_id').val()) , case_name : $.trim($('#case_name').val()) },
              success : function(data){
                //alert(data);
                if (data == 'found' ) {
                  $('#alert').removeClass('hide');
                  $('#alert-success').addClass('hide');
                  $('#alert').find('span').html('มีแฟ้มรายคดีนี้อยู่แล้ว');
                }else{
                  $('#alert').addClass('hide');
                  $('#alert-success').removeClass('hide');
                  $('#alert-success').find('span').html('บันทึกแฟ้มคดีเรียบร้อย');
                }
              },
              error : function(error){
                alert('err : '.error);
              }
            });
          }else{
            $('#alert').removeClass('hide');
            $('#alert-success').addClass('hide');
            $('#alert').find('span').html('กรุณากรอกข้อมูลให้ครบถ้วน');
          }
        });

        $(document).on("click", '.del', function(){ 
          //alert($(this).data('identify'));
          $.ajax({
            type : 'post',
            url : '<?php echo base_url(); ?>index.php/borrowingSystemController/delFileCase',
            data : { id : $(this).data('identify') },
            success : function(data){
              alert(data.indexOf('1'));
              if (data.indexOf('1') > -1 ) {
                getDataList();
                $('#alert-success').removeClass('hide');
                $('#alert-success').find('span').html('ลบรายการเรียบร้อย');
              }else{
                $('#alert').removeClass('hide');
                $('#alert').addClass('hide');
                $('#alert').find('span').html('เกิดข้อผิดพลาด ');
              }
            },
            error : function(err){
              alert(err);
            }
          });
        });
      });

    </script>